C++ Windows Programming by 2016

C++ Windows Programming by 2016

Author:2016
Language: eng
Format: epub, mobi
Publisher: Packt Publishing


The parser – Generating the syntax tree

The user inputs a formula beginning with an equal sign (=). The parser's task is to translate the scanner's token list into a syntax tree. The syntax of a valid formula can be defined by a grammar. Let's start with a grammar that handles expressions that make use of the arithmetic operators:

A grammar is a set of rules. In the preceding grammar, there are eight rules. Formula and Expression are called non-terminals; EndOfLine, Value, and the characters +, -, *, /, (, and ) are called terminals. Terminals and non-terminals are called symbols. One of the rules is the grammar's start rule, in our case the first rule. The symbol to the left of the start rules is called the grammar's start symbol, in our case Formula.

The arrow can be read as "is", and the preceding grammar can be read as:

A formula is an expression followed by end-of-line. An expression is the sum of two expressions, the difference of two expressions, the product of two expressions, the quotient of two expressions, an expression enclosed by parentheses, a reference, or a numerical value.

This is a good start, but there are a few problems. Let's test if the string 1 + 2 * 3 is accepted by the grammar. We can test that by doing a derivation, where we start with the start symbol Formula and apply the rules until there are only terminals. The digits in the following derivation refer to the grammar rules:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.